"use strict";
var _fid, _uName;
var $structure_manage, $structure_relation;

function init() {
	var uid = vw_static.getCookie(window.location.host);
	_fid = vw_static.getQueryString(window, "fid");
	_uName = vw_static.getCookie(uid).split('|')[0];

	$structure_manage = $('#structure_manage');
	$structure_relation = $('#structure_relation');

	var subFunction = [
		'<ul class="nav nav-pills navbar-left" style="margin: 10px 0 0 100px;">',
		'	{{#items}}<li class="{{class}}" style="margin-left: 10px;">',
		'		<a href="#{{tab}}" class="btn btn-custom" data-toggle="tab"><i class="fa fa-{{icon}}"></i> {{name}}</a>',
		'	</li>{{/items}}',
		'</ul>'
	].join('');

	require(['mustache'], function(mustache) {
		$('#vw-navbar-custom').append(mustache.render(subFunction, {
			"items": [{
				"tab": "tab_manage",
				"class": "active",
				"icon": "database",
				"name": "调查对象管理"
			}, {
				"tab": "tab_relation",
				"icon": "sitemap",
				"name": "调查对象关系查看"
			}]
		})).on('click', 'a', function() {
			switch($(this).attr("href")) {
				case "#tab_manage":
					InitTab_Manage();
					break;
				case "#tab_relation":
					InitTab_Relation();
					break;
			}
		});
	});

	InitTab_Manage();
}

function InitTab_Manage() {
	$structure_manage.vw_structure('destroy').vw_structure({
		edittable: {
			showImportData: true,
			showNewData: true,
			showErrorlog: true,
			showEdit: true,
			showExport: true,
			ExtraTools: '<button type="button" id="fn_refresh" class="btn btn-danger"><i class="fa fa-refresh"></i> 刷新</button>',
			onInitialized: function() {
				$('#fn_refresh').click(fn_refresh);
			}
		},
		interfaceAddress: VW_INTERFACE.WEBSERVICE.ObjectManage,
		extrapara: {
			userID: vw_static.getCookie(window.location.host),
			functionID: vw_static.getQueryString(window, "fid")
		},
	});
}

function fn_refresh() {
	vw_common.customAjax({
		url: VW_INTERFACE.WEBSERVICE.ObjectManage('RefreshData'),
		type: "GET",
		dataType: "json",
	}, function() {
		$.Notify({
			caption: "成功",
			content: "调查对象关系已刷新",
			type: "success"
		});
	});
}

function InitTab_Relation() {
	$structure_relation.vw_structure('destroy').vw_structure({
		searchtree: {
			//			menu: [{
			//				id: 1,
			//				text: "按调查对象"
			//			}],
			selectFirst: false,
			onTreeSelected: fn_locate
		},
		showMap: true,
		showEdittable: false,
		interfaceAddress: VW_INTERFACE.WEBSERVICE.ObjectManage,
		treeDataURL: "GetSurveyData"
	});
}

function fn_locate(node) {
	$structure_relation.vw_structure('getMap').append("<div id='mapMask'><div id='mapMaskText'>正在加载数据，请稍候。。。</div></div>")
	$("#mapMask").css({
		"position": "absolute",
		"top": "0",
		"left": "0",
		"width": "100%",
		"height": "100%",
		"background": "#FFFFFF",
		"opacity": "0.3",
		"z-index": "999999"
	});
	$("#mapMaskText").css({
		"position": "absolute",
		"top": "50%",
		"left": "35%",
		"font-size": "28px"
	});

	vw_common.customAjax({
		url: VW_INTERFACE.WEBSERVICE.ObjectManage('GetObject'),
		type: "GET",
		dataType: "json",
		data: {
			"catalogID": node.id
		}
	}, function(e) {
		$structure_relation.vw_structure('getMap').vw_map('refresh').vw_map("drawPolygon", e.data, true);

		if(!node.nodes) {
			vw_common.customAjax({
				url: VW_INTERFACE.WEBSERVICE.ObjectManage('GetNatureLand'),
				type: "GET",
				dataType: "json",
				data: {
					"code": node.id
				}
			}, function(e) {
				$structure_relation.vw_structure('getMap').vw_map("drawPolygon", e.data, false, {
					color: "#FF0000",
					weight: 2
				});
				$("#mapMask").remove();
			});
		} else {
			$.each(node.nodes, function(k, val) {
				vw_common.customAjax({
					url: VW_INTERFACE.WEBSERVICE.ObjectManage('GetObject'),
					type: "GET",
					dataType: "json",
					data: {
						"catalogID": val.id
					}
				}, function(e) {
					$structure_relation.vw_structure('getMap').vw_map("drawPolygon", e.data, false, {
						color: "#FFFACD",
						weight: 2
					});
					$("#mapMask").remove();
				});
			});
		}
	});
}